'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')

function resolve (dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title // page title

// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following methods:
// port = 9528 npm run dev OR npm run dev --port = 9528
const port = process.env.port || process.env.npm_config_port || 8079 // dev port

// All configuration item explanations can be find in https://cli.vuejs.org/config/
// vue.config.js的主体配置
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    // 服务相关的设置
    host: '127.0.0.1', // 指定一个主机名
    port: 8001, // 指定一个端口号
    open: true, // 运行成功后是否自动打开页面
    overlay: {
      warnings: false,
      errors: true
    },
    // proxy: {
    //   // // 代理相关。如果你的前端应用和后端 API 服务器没有运行在同一个主机上，你需要在开发环境下将 API 请求代理到 API 服务器
    //   // '/api': {
    //   //   // 对请求接口中出现的api进行代理
    //   //   target: process.env.VUE_APP_PROXY_URL, // 代理的目标地址，这个取值在后面的文件会讲到
    //   //   changeOrigin: true, // 是否改变域名，
    //   //   ws: false, // 是否开启webSocket
    //   //   pathRewrite: {
    //   //     // 路径重写，如果默认不重写路径，那么`/api/users`会被代理到`target路径/api/users`
    //   //     '^/api': '' // 对api进行路径重写，重写后，那么`/api/users`会被代理到`target路径/users`
    //   //   }
    //   // },
    //   // 真实地址指向http://127.0.0.1:9001/admin
    //   '/api/admin': {
    //     // target: process.env.ADMIN_BASE_URL,
    //     target: "http://127.0.0.1:9001/",
    //     ws: false,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/api/admin': '/inner_api/admin'
    //     }
    //   }
    // }
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    devtool: 'source-map',
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
  },
  chainWebpack (config) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        // to ignore runtime.js
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  }
}
